package org.ttitfly.zuoyue.service.room;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.ttitfly.ttcore.entity.account.User;
import org.ttitfly.ttcore.service.menu.SystemMenuManager;
import org.ttitfly.workflow.Session;
import org.ttitfly.workflow.SessionHolder;
import org.ttitfly.workflow.engine.assignment.Assignment;
import org.ttitfly.workflow.engine.run.Task;
import org.ttitfly.zuoyue.entity.room.RoomProject;

@Component("RoomProjectAssignment")
public class RoomProjectAssignment implements Assignment {

	@Resource
	private SystemMenuManager systemMenuManager;
	
	private static final String GSM_BUILD_TYPE = "新建室外直放站";
	
	@Override
	public String[] assign() {
		Session session = SessionHolder.currentSession();
		Task task = session.currentTask();
		RoomProject p = (RoomProject) session.getVariable("roomProject");
		List<User> users = systemMenuManager.findUsersByWorkflow(task.getProcessInstance().getProcessName(), task.getUnitName(), p.getArea().getId());
		//String[] userStr = new String[users.size()];
		List<String> userList = new ArrayList<String>();
		int i = 0;
		if(users.size() >0){
			for(User user:users){
				//userStr[i++] = user.getLoginName();
				userList.add(user.getLoginName());
			}
		}
		//过滤指派的用户【去掉不必要的用户】
		String netType = p.getNetType();
		String gsmBuildType = p.getGsmBuildType();
		if(netType.indexOf("WLAN") != -1){
			if(userList.contains("wujia")){
				userList.remove("wujia");
			}
			if(userList.contains("pianqu-dong")){
				userList.remove("pianqu-dong");
			}
			if(userList.contains("pianqu-nan")){
				userList.remove("pianqu-nan");
			}
			if(userList.contains("pianqu-xi")){
				userList.remove("pianqu-xi");
			}
			if(userList.contains("pianqu-zhong")){
				userList.remove("pianqu-zhong");
			}
		}else{
			String region = p.getRegion();
			if(userList.contains("zhangminhua")){
				userList.remove("zhangminhua");
			}
			if(gsmBuildType.equalsIgnoreCase(GSM_BUILD_TYPE)){
				if(region.equals("EAST")){
					if(userList.contains("wujia")){
						userList.remove("wujia");
					}
					if(userList.contains("pianqu-nan")){
						userList.remove("pianqu-nan");
					}
					if(userList.contains("pianqu-xi")){
						userList.remove("pianqu-xi");
					}
					if(userList.contains("pianqu-zhong")){
						userList.remove("pianqu-zhong");
					}
				}else if(region.equals("SOUTH")){
					if(userList.contains("wujia")){
						userList.remove("wujia");
					}
					if(userList.contains("pianqu-dong")){
						userList.remove("pianqu-dong");
					}
					if(userList.contains("pianqu-xi")){
						userList.remove("pianqu-xi");
					}
					if(userList.contains("pianqu-zhong")){
						userList.remove("pianqu-zhong");
					}
				}else if(region.equals("WEST")){
					if(userList.contains("wujia")){
						userList.remove("wujia");
					}
					if(userList.contains("pianqu-dong")){
						userList.remove("pianqu-dong");
					}
					if(userList.contains("pianqu-nan")){
						userList.remove("pianqu-nan");
					}
					if(userList.contains("pianqu-zhong")){
						userList.remove("pianqu-zhong");
					}
				}else if(region.equals("MIDDLE")){
					if(userList.contains("wujia")){
						userList.remove("wujia");
					}
					if(userList.contains("pianqu-dong")){
						userList.remove("pianqu-dong");
					}
					if(userList.contains("pianqu-nan")){
						userList.remove("pianqu-nan");
					}
					if(userList.contains("pianqu-xi")){
						userList.remove("pianqu-xi");
					}
				}
			}
		}
		
		String[] userStr = new String[userList.size()];
		for(String userLoginName:userList){
			userStr[i++] = userLoginName;
		}
		
		return userStr;
	}

}
